package com.tfswx.fljdmxyy.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.newbie.core.exception.BusinessException;
import com.tfswx.fljdmxyy.constant.SystemConstant;
import com.tfswx.fljdmxyy.dao.ZzjgXtJsMapper;
import com.tfswx.fljdmxyy.dto.jsgl.*;
import com.tfswx.fljdmxyy.enums.BusinessCodeEnum;
import com.tfswx.fljdmxyy.model.ZzjgXtJs;
import com.tfswx.fljdmxyy.query.jsgl.JsxxQueryResult;
import com.tfswx.fljdmxyy.service.JsglService;
import com.tfswx.fljdmxyy.utils.ConverterUtils;
import com.tfswx.fljdmxyy.utils.UuidUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 角色管理实现类
 *
 * @author 唐晓宇
 * @date 2022/11/11
 */
@Slf4j
@Service
public class JsglServiceImpl implements JsglService {

    @Resource
    private ZzjgXtJsMapper zzjgXtJsMapper;

    @Override
    public Boolean updateJsxx(JsxxUpdateInputDTO dto) {
        ZzjgXtJs jsxx = ConverterUtils.convert(dto, ZzjgXtJs.class);
        jsxx.setZhxgsj(new Date());
        List<String> gnbmList = dto.getGnbm();
        // 1.如果功能编码不为空
        if (CollectionUtil.isNotEmpty(gnbmList)) {
            jsxx.setGnbm(StrUtil.join(",", gnbmList));
        }

        // 2.修改角色信息
        Integer result = zzjgXtJsMapper.updateZzjgXtJs(jsxx);
        if (result <= 0) {
            throw new BusinessException(BusinessCodeEnum.XGSJSB);
        }
        return Boolean.TRUE;
    }

    @Override
    public List<JsxxQueryResult> queryJsxx() {
        List<JsxxQueryResult> result = new ArrayList<>();
        // 1.查询角色信息
        List<ZzjgXtJs> jsList = zzjgXtJsMapper.queryZzjgXtJs(null);

        // 2.查询功能权限
        if (CollectionUtil.isNotEmpty(jsList)) {
            jsList.forEach(js -> {
                JsxxQueryResult jsxxQueryResult = ConverterUtils.convert(js, JsxxQueryResult.class);
                if (StrUtil.isNotBlank(js.getGnbm())) {
                    jsxxQueryResult.setGnbm(Arrays.asList(js.getGnbm().split(",")));
                }
                result.add(jsxxQueryResult);
            });
        }
        return result;
    }

    @Override
    public Boolean deleteJsxx(JsxxDeleteInputDTO dto) {
        ZzjgXtJs jsxx = ConverterUtils.convert(dto, ZzjgXtJs.class);
        jsxx.setZhxgsj(new Date());
        Integer result = zzjgXtJsMapper.deleteZzjgXtJs(jsxx);
        if (result <= 0) {
            throw new BusinessException(BusinessCodeEnum.SCSJSB);
        }
        return Boolean.TRUE;
    }

    @Override
    public JsxxAddOutputDTO addJsxx(JsxxAddInputDTO dto) {
        ZzjgXtJs jsxx = ConverterUtils.convert(dto, ZzjgXtJs.class);
        Date now = new Date();
        jsxx.setJsbm(UuidUtils.uuid32());
        jsxx.setCjsj(now);
        jsxx.setZhxgsj(now);
        jsxx.setSfsc(SystemConstant.N);
        // 1.功能编码转为字符串
        String gnbm = StrUtil.join(",", dto.getGnbm());
        jsxx.setGnbm(gnbm);

        // 2.保存角色信息
        Integer result = zzjgXtJsMapper.insertZzjgXtJs(jsxx);
        if (result <= 0) {
            throw new BusinessException(BusinessCodeEnum.TJSJSB);
        }
        return ConverterUtils.convert(jsxx, JsxxAddOutputDTO.class);
    }

    @Override
    public JsxxGetOutputDTO getJsxx(JsxxGetInputDTO dto) {
        ZzjgXtJs jsxx = ConverterUtils.convert(dto, ZzjgXtJs.class);
        // 1.获取角色信息
        jsxx = zzjgXtJsMapper.getZzjgXtJs(jsxx);
        JsxxGetOutputDTO outputDTO = ConverterUtils.convert(jsxx, JsxxGetOutputDTO.class);

        // 2.如果功能编码不为空，则设置功能编码
        if (ObjectUtil.isNotNull(jsxx) && CharSequenceUtil.isNotBlank(jsxx.getGnbm())) {
            outputDTO.setGnbm(Arrays.asList(jsxx.getGnbm().split(",")));
        }
        return outputDTO;
    }
}
